package edu.ncsu.csc.camera.com;

import java.lang.StringBuffer;

public class ByteVector
{
	private byte[] data;
	private int capacity;
	private int elements;

    public int getSize() {
        return elements;
    }

    public int getCurrentCapacity() {
        return capacity;
    }

	public ByteVector(byte[] b)
	{
		data = new byte[b.length];
		capacity = b.length;
		elements = capacity;
		for(int k = 0; k < elements; k++)
			data[k] = b[k];
	}

	public ByteVector(int initial_size)
	{
		capacity = initial_size;
		data = new byte[capacity];
		elements = 0;
	}
	
	public void add(byte b)
	{
		if(elements < capacity)
			data[elements] = b;
		else
		{
			capacity *= 2;
			byte[] new_data = new byte[capacity];
			for(int k = 0; k < elements; k++)
				new_data[k] = data[k];
			new_data[elements] = b;
			data = new_data;
		}
		elements++;
	}
	
	public byte get(int i)
	{
		return data[i];
	}
	
	public void add(char c)
	{
		add((byte)c);
	}
	
	public int getLength()
	{
		return elements;
	}

	/* only use this method for print ASCII!
		I'm including an extra '\n' every time I get a '\r'
		for the literal response use writeByteArray()
	*/
	
	public String toString()
	{
		StringBuffer buffer = new StringBuffer(elements);
		for(int k = 0; k < elements; k++)
		{
			buffer.append((char)data[k]);
			if(data[k] == '\r')
				buffer.append('\n');
		}
		return buffer.toString();
	}
}